class WinFreeNum extends egret.DisplayObjectContainer {
	number: number;
	private speed = .05;
	private context;
	private timeoutindex:number = 0;
	
	public constructor(context: any) {
		super();
		this.context = context;
		this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
	}
	public setNumber(number: number) {
		this.number = number;
	}
	private onAddToStage(event: egret.Event) {
		new Bg(this, 'mark_in_free_png');
		var percentage = 0.65;

		this.touchEnabled = true;
		this.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onClose, this);

		//加载主面板
		var imgCon = [];
		imgCon['url'] = 'win_free_bg_png';
		imgCon['width'] = this.stage.stageWidth * 0.5;
		imgCon['height'] = this.stage.stageHeight * 0.6;
		imgCon['y'] = this.stage.stageHeight * 0.15;
		imgCon['x'] = (this.stage.stageWidth -imgCon['width'])/2+50;
		this.addChild(new ImageBitmap(imgCon).getImgPoint());

		/**
		   * 数字
		   */
		// var switchPaCon = [];
		// switchPaCon['url'] = 'china_' + this.number + '_png';
		// switchPaCon['width'] = this.stage.stageWidth * 0.03;
		// switchPaCon['height'] = this.stage.stageHeight * 0.03 * 1920 / 1080;
		// switchPaCon['y'] = this.stage.stageHeight * 0.585;
		// switchPaCon['x'] = this.stage.stageWidth * 0.445;
		// this.addChild(new ImageBitmap(switchPaCon).getImgPoint());

	/**
		   * 数字
		   */
		var switchPaCon = new NumPanel(20,30,"y_");

		switchPaCon.y = this.stage.stageHeight * 0.415;
		switchPaCon.x = this.stage.stageWidth * 0.415;
		if(this.number>9){
		switchPaCon.x = this.stage.stageWidth * 0.43;
		}
		this.addChild(switchPaCon);
		switchPaCon.setNum(this.number);


		this.alpha = 0;
		this.openAm();

		this.timeoutindex = egret.setTimeout(function () {
			this.onClose();
		}, this,5000);

	}
	public openAm(): void {
		this.removeEventListener(egret.Event.ENTER_FRAME, this.enterFrameHandler, this);
		this.addEventListener(egret.Event.ENTER_FRAME, this.enterFrameHandler, this);
	}
	public closeAm(): void {
		this.removeEventListener(egret.Event.ENTER_FRAME, this.closeFrameHandler, this);
		this.addEventListener(egret.Event.ENTER_FRAME, this.closeFrameHandler, this);
	}
	/**打开时逐帧运动*/
	private enterFrameHandler(event: egret.Event): void {
		if (this.alpha < 1) {
			this.alpha += this.speed;
		} else {
			this.pauseOpenAm();
		}
	}

	/**
	 * 关闭时动画运动
	 * @param event
	 */
	private closeFrameHandler(event: egret.Event): void {
		if (this.alpha > 0) {
			this.alpha -= this.speed;
		} else {
			this.pauseOpenAm();
			if (this.parent) {
				this.parent.removeChild(this);
				this.context.complete();
			}
		}
	}
	/**暂停打开时动画*/
	public pauseOpenAm(): void {
		this.removeEventListener(egret.Event.ENTER_FRAME, this.enterFrameHandler, this);
	}

	/**暂停关闭时的动画*/
	public pauseCloseAm(): void {
		this.removeEventListener(egret.Event.ENTER_FRAME, this.closeFrameHandler, this);
	}

	private onClose() {
		this.closeAm();
		if(this.timeoutindex){
			egret.clearTimeout(this.timeoutindex);
		}
	}
}